/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.dao;

import com.myapp.struts.actionform.UOMActionForm;
import com.myapp.struts.beans.UomBean;
import com.myapp.struts.prepare.PrepareUom;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.struts.action.ActionForm;

/**
 *
 * @author pavan
 */
public class UomDao {

    private Connect drv;
    private Connection con;
    private Vector vec;
    private String getType;
    private PreparedStatement pstmt;
    private ResultSet uoms;
    private PrepareUom puom;
    private Vector uomList;
    private int status;
    private UOMActionForm form1;

    public UomDao() {
        uomList = new Vector();
        vec = new Vector();
        puom = new PrepareUom();
    }

    public Connection getConnect() throws Exception {
        drv = new Connect();
        con = drv.mySql();
        vec = new Vector();
        return con;
    }

    public Vector getAllUoms() {
        try {
            con = getConnect();
            String uomQ = "select distinct(uom) from uom;";
            pstmt = con.prepareStatement(uomQ);
            uoms = pstmt.executeQuery();
            uomList = puom.prepareUomList(uoms);
            con.close();
            pstmt.close();
        } catch (Exception ex) {
            Logger.getLogger(UomDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return uomList;
    }

    public Vector getRows() {
        try {
            con = getConnect();
            String uomQ = "select * from uom;";
            pstmt = con.prepareStatement(uomQ);
            uoms = pstmt.executeQuery();
            uomList = puom.prepareRows(uoms);
            con.close();
            pstmt.close();
        } catch (Exception ex) {
            Logger.getLogger(UomDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return uomList;
    }

    public boolean insert(ActionForm form) {
        try {
            /** Type cast ActionForm variable to FormBean */
            form1 = (UOMActionForm) form;

            /** Take Database Connection */
            con = getConnect();

            pstmt = con.prepareStatement("INSERT INTO uom VALUES(?,?)");
            pstmt.setString(1, form1.getUomName());
            pstmt.setString(2, form1.getDescription());
            status = pstmt.executeUpdate();

            con.close();
            pstmt.close();
            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(UomDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean delete(UOMActionForm ubean) {
        try {
            con = getConnect();
            pstmt = con.prepareStatement("delete from uom where uom=?");
            pstmt.setString(1, ubean.getUomName());
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();
            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(UomDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean editUom(String sourceuom, UOMActionForm ubean) {
        try {
            con = getConnect();
            pstmt = con.prepareStatement("update uom set uom=? where uom=?");
            pstmt.setString(1, ubean.getUomName());
            pstmt.setString(2, sourceuom);
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();
            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(UomDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
